package com.etc.logManage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.common.exception.DataOperationException;
import com.etc.common.exception.SysOperationException;
import com.etc.common.util.CustomBeanUtils;
import com.etc.logManage.dao.ILogOperationMapper;
import com.etc.logManage.model.LogOperationModel;
import com.etc.logManage.service.ILogOperationService;
import com.etc.logManage.vo.LogOperationVO;
import com.github.pagehelper.PageHelper;

/**
 * @description
 * @author 黄宇 E-mail: yuanddan2006@126.com
 * @date 创建时间：2016年3月26日 上午11:23:35
 * @version 1.0
 */
@Service("logOperationService")
public class LogOperationServiceImpl implements ILogOperationService {

	@Autowired
	private ILogOperationMapper logOperationMapper;

	@Override
	public Integer insert(LogOperationVO vo) {
		try {
			LogOperationModel model = new LogOperationModel();
			CustomBeanUtils.copyProperties(model, vo);
			return this.logOperationMapper.insert(model);
		} catch (Exception e) {
			String errMsg = String.format("LogOperation保存异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public void delete(Integer id) {
		try {
			this.logOperationMapper.delete(id);
		} catch (Exception e) {
			String errMsg = String.format("LogOperation删除异常,参数 :id=%s", id);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public void update(LogOperationVO vo) {
		try {
			LogOperationModel model = new LogOperationModel();
			CustomBeanUtils.copyProperties(model, vo);
			this.logOperationMapper.update(model);
		} catch (Exception e) {
			String errMsg = String.format("LogOperation更新异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg, e);
		}
	}

	@Override
	public LogOperationVO selectById(Integer id) {
		LogOperationVO vo = new LogOperationVO();
		try {
			LogOperationModel model = this.logOperationMapper.selectById(id);
			CustomBeanUtils.copyProperties(vo, model);
			return vo;
		} catch (Exception e) {
			String errMsg = String.format("logOperation查询异常,参数:id=%s", id);
			throw new SysOperationException(errMsg,e);
		}
	}

	@Override
	public List<LogOperationVO> list(Map<String, Object> param) {
		try{
			PageHelper.startPage((Integer) param.get("pageNo"), (Integer) param.get("pageNum"));
			List<LogOperationModel> modelList = this.logOperationMapper.list(param);
			List<LogOperationVO> voList = new ArrayList<LogOperationVO>();
	
			for (LogOperationModel model : modelList) {
				LogOperationVO vo = new LogOperationVO();
				CustomBeanUtils.copyProperties(vo, model);
				voList.add(vo);
			}
			return voList;
		} catch (Exception e) {
			String errMsg = String.format("logOperation查询异常,参数:param=%s", param);
			throw new SysOperationException(errMsg,e);
		}
	}

	@Override
	public int count(Map<String, Object> param) {
		return this.logOperationMapper.count(param);
	}

}
